package demo.letcode._初级算法;

import java.util.Arrays;

/**
 * 给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数。
 *
 * 示例 1:
 * 输入: nums = [1,2,3,4,5,6,7], k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右旋转 1 步: [7,1,2,3,4,5,6]
 * 向右旋转 2 步: [6,7,1,2,3,4,5]
 * 向右旋转 3 步: [5,6,7,1,2,3,4]
 * 示例 2:
 * 输入：nums = [-1,-100,3,99], k = 2
 * 输出：[3,99,-1,-100]
 * 解释:
 * 向右旋转 1 步: [99,-1,-100,3]
 * 向右旋转 2 步: [3,99,-1,-100]
 *
 * @author safe
 * @date 2021/3/17
 */
public class _旋转数组 {
    public static void main(String[] args) {
        int a[] = {1, 2, 3, 4, 5, 6, 7};
        rotate(a,4);
        System.out.println(Arrays.toString(a));
    }

    private static void rotate(int[] nums, int k) {
        int start = 0;
        int cur = nums[0];
        int pre;

        //  int a[] = {1, 2, 3, 4, 5, 6, 7};
        // [5, 6, 7, 1, 2, 3, 4]
        int index = 0;
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            pre = cur;
            index = (index + k) % len;// 获取偏移索引
            cur = nums[index];
            nums[index] = pre;
            System.out.println(Arrays.toString(nums));
            if (index == start) {
                // index = ++start;
                // if (index < len) {
                //     cur = nums[index];
                // } else {
                    return;
                // }
            }
        }
    }
}
